<?php
  require_once("../conf/conf.inc.php");
  require_once("../lib/util.lib.php");
  require_once("class/Champ.class.php");
  validerLogin();
  $request = $_POST;
  if(!isset($request["id_evenement"]) && !isset($_GET["id_evenement"]))
    die("Vous ne pouvez acc&eacute;der cette page sans avoir de id_evenement");
    
  $request["id"] = (isset($request["id"])?
              $request["id"]:
                (isset($_GET["id"])?$_GET["id"] : "new")
             );
  $request["id_evenement"] = (isset($request["id_evenement"])?
              $request["id_evenement"]:$_GET["id_evenement"]);             

  if(isset($_GET["supprimer"])){
    Champ::supprimerChamp($request["id"]);
    header("Location:edit_evenement.php?id={$request["id_evenement"]}");
  }
  if (isset($request["edit_champ"])){
    $message = validerEdit($request);
    if($message == ''){
      if($request["id"] == "new"){
      
        $request["id"] = Champ::ajouterChamp($request["id_evenement"],
                          $request["etiquette"],
                          $request["type"],
                          $request["multiple"],
                          $request["selection"],
                          $request["poids"]);
        
      }
      else{
        Champ::modifierChamp(   $request["id"],
                    $request["etiquette"],
                    $request["type"],
                    $request["multiple"],
                    $request["selection"],
                    $request["poids"]);
      }
      header("Location:edit_evenement.php?id={$request["id_evenement"]}");
    }
    else
      echo $message;
    
  }
  else{
    
    chargerChamp($request);
  }

  function chargerChamp(&$request){
    if($request["id"] != "new"){
      $request = Champ::getChamp($request["id"]);
    }
    else{
      $request["etiquette"] = "";
      $request["type"] = 1;
      $request["multiple"] = 0;
      $request["selection"] = "";
      $request["poids"] = 0;
    }
  }
  
  /**
   * Valide si le post est valide en fonction du formulaire. 
   * Les dates dans le post sont converties en timestamp
   *
   * @param Array $post
   * @return string
   */
  function validerEdit(&$post){
    if (!isset($post["etiquette"]) || $post["etiquette"] == "")
      return "L'&eacute;tiquette du champ est obligatoire";
  
    if ($post["type"] > 4 && (!isset($post["selection"]) && $post["selection"] == ""))
      return "Lorsque la case 'Liste de valeur est visible, elle est obligatoire";
    if (!isset($post["selection"]))
      $post["selection"] = "";
    if (!isset($post["multiple"]))
        $post["multiple"] = 0;
    return '';
  }
?>
<html>
  <head>
  <script type="text/javascript" src="../lib/jquery-1.2.3.min.js"></script>
  <script type="text/javascript">
      var row_style ="";
        if(navigator.userAgent.indexOf('MSIE') == -1)
          row_style = "table-row";
        else
          row_style = 'block';
      
      function afficherOptions(select){
        if (select.value > 5)
          document.getElementById("options").style.display = row_style;
        else
          document.getElementById("options").style.display = "none";
      }
      $(document).ready(function() {
        $("#type").trigger("change");//initialiser la boite des valeurs
      });
    </script>
  </head>
  <body>
    <table align="center" class="edition">
    <tr><td align="center">
      &Eacute;dition d'un champ
      <form action="edit_champ.php" method="post">
      <table>
        <tr><td>
          &Eacute;tiquette&nbsp;:
        </td><td>
          <input type="text" name="etiquette" value="<?php echo $request["etiquette"] ?>" /> 
          
        </td></tr>
        <tr><td>
          Poids&nbsp;:
        </td><td>
          <select name="poids">
            <option <?php echo (($request["poids"] == "-5")?"selected='selected'":""); ?> value="-5">-5</option>
            <option <?php echo (($request["poids"] == "-4")?"selected='selected'":""); ?> value="-4">-4</option>
            <option <?php echo (($request["poids"] == "-3")?"selected='selected'":""); ?> value="-3">-3</option>
            <option <?php echo (($request["poids"] == "-2")?"selected='selected'":""); ?> value="-2">-2</option>
            <option <?php echo (($request["poids"] == "-1")?"selected='selected'":""); ?> value="-1">-1</option>
            <option <?php echo (($request["poids"] == "0")?"selected='selected'":""); ?> value="0">0</option>
            <option <?php echo (($request["poids"] == "1")?"selected='selected'":""); ?> value="1">1</option>
            <option <?php echo (($request["poids"] == "2")?"selected='selected'":""); ?> value="2">2</option>
            <option <?php echo (($request["poids"] == "3")?"selected='selected'":""); ?> value="3">3</option>
            <option <?php echo (($request["poids"] == "4")?"selected='selected'":""); ?> value="4">4</option>
            <option <?php echo (($request["poids"] == "5")?"selected='selected'":""); ?> value="5">5</option>
          </select>
        </td></tr>
        <tr><td>
          Type de champs&nbsp;:
        </td><td>
          <select name="type" id="type" onchange="afficherOptions(this)" onkeyup="afficherOptions(this)">
            <option value="1" <?php echo (($request["type"] == "1")?"selected='selected'":""); ?> ><?php echo getEtiquetteType(1);?></option>
            <option value="2" <?php echo (($request["type"] == "2")?"selected='selected'":""); ?>><?php echo getEtiquetteType(2);?></option>
            <option value="3" <?php echo (($request["type"] == "3")?"selected='selected'":""); ?>><?php echo getEtiquetteType(3);?></option>
            <option value="4" <?php echo (($request["type"] == "4")?"selected='selected'":""); ?>><?php echo getEtiquetteType(4);?></option>
            <option value="5" <?php echo (($request["type"] == "5")?"selected='selected'":""); ?>><?php echo getEtiquetteType(5);?></option>
            <option value="6" <?php echo (($request["type"] == "6")?"selected='selected'":""); ?>><?php echo getEtiquetteType(6);?></option>
            <option value="7" <?php echo (($request["type"] == "7")?"selected='selected'":""); ?>><?php echo getEtiquetteType(7);?></option>
            <option value="8" <?php echo (($request["type"] == "8")?"selected='selected'":""); ?>><?php echo getEtiquetteType(8);?></option>
          </select>
        </td></tr>
        <tr><td>Sélection multiple:</td><td><input type="checkbox" name="multiple" <?php echo ($request["multiple"]? "checked='checked'":""); ?> value="1" /></td></tr>
        <tr id="options" style="display:none" valign="top"><td>
          Liste des valeurs&nbsp;:
        </td><td>
          <div>
            <textarea name="selection" rows="6" width="100%"><?php echo str_replace("<br />","\n",$request["selection"]) ?></textarea>
          </div>
        </td></tr>
        <tr><td align="center" colspan="2">
          <input type="hidden" name="id_evenement" value="<?php echo $request["id_evenement"]; ?>" />
          <input type="hidden" name="id" value="<?php echo $request["id"]; ?>" />
          <input type="hidden" name="edit_champ" value="1" />
          <button type="submit">Appliquer</button>
          <br />
          <br />
          <a href="edit_evenement.php?id=<?php echo $request["id_evenement"]; ?>">Retour</a>
        </td></tr>
      </table>
      </form>
    </td></tr>
    </table>  
  </body>
</html>